#include "mode_brighness_config.h"


u8 brighness_level;

void init_brighness_level(u8 level)
{
    if (level <MIN_BRIGH)
    {
        brighness_level =MIN_BRIGH;
    }
    else if(brighness_level>MAX_BRIGHT)
    {
        brighness_level=MAX_BRIGHT;
    }
    else
    {   
        brighness_level=level;
    }
    LOG_INFO("brighness_level is%d\n",brighness_level);
}

void set_brighness_level(u8 level)
{
    if (level <MIN_BRIGH)
    {
        brighness_level =MIN_BRIGH;
    }
    else if(brighness_level>MAX_BRIGHT)
    {
        brighness_level=MAX_BRIGHT;
    }
    else
    {   
        brighness_level=level;
    }
    LOG_INFO("brighness_level is%d\n",brighness_level);
}

u8 get_brighness_level()
{
    return brighness_level;
}

void reduce_brighness_level(u8 reduce_level)  //降低灯管亮度
{
    if (brighness_level<reduce_level)//最小不能小于0
    {
       brighness_level=0; 
    }
    else 
    {
        brighness_level =brighness_level-reduce_level;
    }
    LOG_INFO("brighness_level is%d\n",brighness_level);
}

void add_brighness_level(u8 add_level)//增加灯光亮度
{
    if(brighness_level+add_level>MAX_BRIGHT)
    {
        brighness_level=MAX_BRIGHT;
    }
    else
    {
        brighness_level = brighness_level+add_level;
    }
    LOG_INFO("brighness_level is%d\n",brighness_level);
}

void once_brilliance_handle(SYS_OPT_KEY opt)
{
    if(OPT_GO_ADD==opt.oepation)
    {
        add_brighness_level(LIGHT_LEVEL);
    }
    else if(OPT_GO_REDUCE==opt.oepation)
    {
        reduce_brighness_level(LIGHT_LEVEL);
    }
}



void continue_brilliance_handle(SYS_OPT_KEY opt)
{
    static u8 flag=0;
    static int time;

    if(OPT_GO_ADD_CONTINUE == opt.oepation)//进入无级加
    {
        if(0==flag)
        {
            flag=2;
            time = arch_os_ms_now();
        }
    }
    else if(OPT_GO_REDUCE_CONTINUE == opt.oepation)  //进入无级减
    {
        if(0==flag)
        {
            flag=1;
            time = arch_os_ms_now();
        }
    }

    if (arch_os_ms_elapsed(time)>=100) //每100ms进入一次
    {
        time = arch_os_ms_now();
        if (1==flag) 
        {   
            if(LEV_KEY_PRESSED == device_gpio_get(GPIO_KEY3_OPT)) //检测按键是否被按下
            {
                reduce_brighness_level(LIGHT_CONTINUE);
            }
            else
            {
                flag=0;
            }
        }
        else if(2==flag) 
        {
            if(LEV_KEY_PRESSED == device_gpio_get(GPIO_KEY4_OPT)) //检测按键是否被按下
            {
                add_brighness_level(LIGHT_CONTINUE);
            }
            else
            {
                flag=0;
            }
        }
    }
}

void brilliance_control_handle(SYS_OPT_KEY opt)
{
    once_brilliance_handle(opt);
    continue_brilliance_handle(opt);
}



